/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.edu.unicen.twitter.suggest.backend.datasources;

import ar.edu.unicen.twitter.suggest.backend.datasources.dao.TwitterDao;
import ar.edu.unicen.twitter.suggest.backend.datasources.twitterservices.TwitterClientImpl;
import ar.edu.unicen.twitter.suggest.backend.dtos.UsuarioProxy;
import ar.edu.unicen.twitter.suggest.backend.dtos.TweetApi;
import ar.edu.unicen.twitter.suggest.commons.dtos.Usuario;
import ar.edu.unicen.twitter.suggest.backend.dtos.UsuarioApi;
import ar.edu.unicen.twitter.suggest.backend.dtos.UsuarioBasico;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.RejectedException;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.TwitterException;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.UserNotFoundException;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.UserProtectedException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class TwitterFacadeImpl implements TwitterFacade {

 private static final Logger LOGGER = Logger.getLogger(TwitterFacadeImpl.class.getName());

    @Autowired
    TwitterDao twitterDao;
    @Autowired
    TwitterClientImpl twitterClient;

    private int maxLinks = 20000;
    /**
     * devuelve los ultimos 20 tweets de la cuenta identificada por
     *
     * @param ID.
     * @return lista de tweets
     */
    @Override
    public List<TweetApi> getNewTweets(String screenName) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        return twitterClient.getNewTweets(screenName);
    }

    /**
     * devuelve todos los tweets posibles de la cuenta
     *
     * @param ID.
     *
     */
    @Override
    public List<TweetApi> getAllTweets(String screenName, int page) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        return twitterClient.getAllTweets(screenName,page);
    }

    @Override
    public long[] getFollows(String screenName,int count) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        //TODO implementar
        return twitterClient.getFollows(screenName,Math.min(getMaxLinks(),count));
    }

    @Override
    public long[] getFollowsFirsts(String screenName) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        //TODO implementar
        return twitterClient.getFollows(screenName,5000);
    }

    @Override
    public long[] getFollowersFirsts(String screenName) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        //TODO implementar
        return twitterClient.getFollowers(screenName,5000);
    }

    @Override
    public long[] getFollowers(String screenName,int count) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        //TODO implementar
        return twitterClient.getFollowers(screenName,Math.min(getMaxLinks(),count));
    }

    @Override
    public UsuarioBasico getUserData(Long id) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        //TODO implementar
        return twitterClient.getUserData(id);
    }

    @Override
    public UsuarioBasico getUserData(String screenName) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
//        return twitterDao.getUserData(screenName);
        return twitterClient.getUserData(screenName);
    }

    @Override
    public Usuario getUser(long id) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        if(id == 0)
            throw new UserNotFoundException("No user with ID = '"+id+"'");
        else
            return new UsuarioProxy(id, this);
    }

    @Override
    public Usuario getUser(String screenName) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        return new UsuarioProxy(screenName, this);
    }

    @Override
    public List<Usuario> getUserList(long[] ids) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        List<Usuario> result = new ArrayList<Usuario>();
        if (ids != null) {
            for (int id = 0; id < ids.length; id++) {
                try
                {
                    Usuario user = getUser(ids[id]);
                    result.add(user);
                }catch(UserNotFoundException e)
                {
                    LOGGER.log(Level.WARNING, "Usuario invalido");
                }
            }
        }
        return result;
    }

    @Override
    public void update(UsuarioApi u) {
       twitterDao.update(u);
    }

    @Override
    public UsuarioApi getUserApi(long id) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        UsuarioApi result = twitterDao.getUsuario(id);
        if (result == null) {
            //si el usuario se encuentra en la base, devuelve uno vacio
            result = new UsuarioApi(id);
        }
        return result;
    }

    @Override
    public UsuarioApi getUserApi(String name) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        UsuarioApi result = null;//twitterDao.getUsuario(name);
        if (result == null) {
            //si el usuario se encuentra en la base, lo pide a twitter
            UsuarioBasico usuarioBasico = getUserData(name);
            result = new UsuarioApi(usuarioBasico);

        }
        return result;
    }

    public int getMaxLinks() {
        return maxLinks;
    }

    public void setMaxLinks(int maxLinks) {
        this.maxLinks = maxLinks;
    }
}
